package com.medical.admin.cost.service.Impl;

import com.medical.admin.cost.common.dto.DetailInfo;
import com.medical.admin.cost.common.dto.IndentDto;
import com.medical.admin.patient.common.dto.PatientDto;
import com.medical.admin.cost.common.dto.TreatmentInfo;
import com.medical.admin.cost.entity.Indent;
import com.medical.admin.cost.ex.IndentException;
import com.medical.admin.cost.mapper.IndentMapper;
import com.medical.admin.cost.service.IndentService;
import com.medical.admin.patient.service.PatientService;
import com.medical.admin.cost.service.TreatmentService;
import com.qf.medical.ex.ServiceException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class IndentServiceImpl implements IndentService {
    @Resource
    private IndentMapper indentMapper;
    @Resource
    private PatientService patientService;
    @Resource
    private TreatmentService treatmentService;

    @Override
    public IndentDto research(Long patientId) {
        IndentDto indentDto = BeanConvertUtils.convertTo(indentMapper.selectByPatenetId(patientId), IndentDto::new);
        if (ObjectUtils.isEmpty(indentDto)){
            throw new IndentException(ResponseCode.INDENT_SELECT_ERROR);
        }
        return indentDto;
    }

    @Override
    public Integer addIndent(IndentDto indentDto) {
        //判断输入金额是否有误
        if (indentDto.getTotalAmount() <=0 ){
            throw new IndentException(ResponseCode.INDENT_AMOUNT_ERROR);
        }
        //添加
        int count = indentMapper.insert(BeanConvertUtils.convertTo(indentDto, Indent::new));
        if (count <= 0){
            throw new IndentException(ResponseCode.INDENT_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer modifyIndent(IndentDto indentDto) {
        int count = indentMapper.updateByPatientId(BeanConvertUtils.convertTo(indentDto, Indent::new));
        if (count <= 0){
            throw new IndentException(ResponseCode.INDENT_ADD_ERROR);
        }
        return count;
    }

    @Override
    public Integer removeIndent(Long patient) {
        int count = indentMapper.deleteByPatientId(patient);
        if (count <= 0){
            throw new IndentException(ResponseCode.INDENT_DELETE_ERROR);
        }
        return count;
    }

    @Override
    public DetailInfo detailIndent(Long patientId) {
        //根据患者id查询患者信息
        PatientDto patientDto = patientService.researchPatient(patientId);
        if (ObjectUtils.isEmpty(patientDto)){
            throw new IndentException(ResponseCode.PATIENT_NULL_ERROR);
        }
        //根据患者id查询订单
        Indent indent = indentMapper.selectByPatenetId(patientId);
        if (ObjectUtils.isEmpty(indent)){
            throw new IndentException(ResponseCode.INDENT_SELECT_ERROR);
        }
        //根据订单查询相关的医院，科室，医生等信息
        TreatmentInfo treatmentInfo = treatmentService.researchByIndentId(indent.getIndentId());
        if (ObjectUtils.isEmpty(treatmentInfo)) {
            throw new ServiceException(ResponseCode.TREATMENT_SELECT_ERROR);
        }

        DetailInfo detailInfo = new DetailInfo();
        detailInfo.setPatientDto(patientDto);
        detailInfo.setTreatmentInfo(treatmentInfo);
        detailInfo.setIndentDto(BeanConvertUtils.convertTo(indent, IndentDto::new));

        return detailInfo;
    }
}
